import scrapy
import re
from dishu.items import CategoryItem
from dishu.items import RecipeItem
from scrapy.http import Request
from urllib import parse
import requests

class dishu(scrapy.Spider):
	name = "dishu"
	allowed_domain = ["xiachufang.com"]

	def start_requests(self):
		urls = ['https://www.xiachufang.com/category/']
		for url in urls:
			yield scrapy.Request(url=url, callback=self.parse)

	def parse(self, response):
		cats = response.css('.cates-list')#大分类 （热门专题、烘焙、肉、果蔬等）数组
		for cat in cats:
			#获取大分类的名字
			topCategoryName = cat.css('.cates-list-info h3::text').extract()[0] 
			#获取小分类
			middleCates = cat.css('.cates-list-all h4')
			smallCates = cat.css('.cates-list-all ul')
			middleCatesLen = len(middleCates)
			for index in range(0,middleCatesLen):
				#获取到每个小分类
				middleCate = middleCates[index]
				#获取到每个小分类的子分类数组
				smallCate = smallCates[index].css('li a')
				#获取小分类的名字
				middleCateName = (middleCate.css('h4::text').extract()[0]).replace(' ','').replace('\n','')

				if middleCateName.strip() == '':
					middleCateName = (middleCate.css('h4 a::text').extract()[0]).replace(' ','').replace('\n','')
				#获取小分类的子分类的名字和分类id
				smallCateLen = len(smallCate)

				if smallCateLen == 0:
					categoryId = middleCate.css('h4::attr(id)').extract()[0]
					categoryId = re.findall(r'\d+\d*',categoryId)[0]
					#创建分类对象
					cateItem =  self.createCateItem(topCategoryName, middleCateName, middleCateName, categoryId)
					yield cateItem

					categoryUrl = 'https://www.xiachufang.com/category/@@@/'.replace('@@@',categoryId)
					yield Request(categoryUrl, callback=self.parseDetail)
				else:
					for i in range(0,smallCateLen):
						cate = smallCate[i]
						categoryName = cate.css('a::text').extract()[0]
						categoryId = cate.css('a::attr(href)').extract()[0]
						categoryId = re.findall(r'\d+\d*',categoryId)[0]
						#创建分类对象
						cateItem = self.createCateItem(topCategoryName, middleCateName, categoryName, categoryId)
						yield cateItem

						categoryUrl = 'https://www.xiachufang.com/category/@@@/'.replace('@@@',categoryId)
						yield Request(categoryUrl, callback=self.parseDetail)

	def parseDetail(self, response):
		recipeList = response.css('.ing-recipe .normal-recipe-list ul li')
		recipeListLen = len(recipeList)
		print(recipeListLen)
		if recipeListLen == 0:
			recipeList = response.css('.normal-recipe-list ul li')
			recipeListLen = len(recipeList)
		if recipeListLen == 0:
			return
		for index in range(0,recipeListLen):
			recipe = recipeList[index]
			recipeId = recipe.css('.recipe div').css('.name a::attr(href)').extract()[0]
			recipeId = re.findall(r'\d+\d*',recipeId)[0]
			recipeName = recipe.css('.recipe div').css('.name a::text').extract()[0].replace(' ','').replace('\n','')
			texts = recipe.css('.recipe div').css('.stats span::text').extract()
			textsLen = len(texts)
			if textsLen == 2:
				recipePoint = texts[0]
				recipeCount = texts[1]
			elif textsLen == 1:
				recipePoint = texts[0]
				recipeCount = ''
			recipeAuthor = recipe.css('.recipe div').css('.author a::text').extract()[0]
			recipeCoverUrl = recipe.css('.recipe img::attr(data-src)').extract()[0]
			categoryIds = recipe.css('.recipe div').css('.ing a::attr(href)').extract()
			categoryIdsLen = len(categoryIds)
			newCategoryIds = []
			for j in range(0,categoryIdsLen):
				aa = categoryIds[j]
				newCategoryIds.append(re.findall(r'\d+\d*',aa)[0])
			recipeItem = self.createRecipeItem(recipeId, newCategoryIds, recipeName, recipeCoverUrl, recipePoint, recipeCount, recipeAuthor)
			yield recipeItem
			categorys = recipe.css('.recipe div').css('.ing a')
			categoryLen = len(categorys)
			for i in range(0,categoryLen):
				category = categorys[i]
				categoryId = category.css('::attr(href)').extract_first()
				categoryId = re.findall(r'\d+\d*',categoryId)[0]
				categoryName = category.css('::text').extract_first()
				categoryItem = self.createCateItem('', '', categoryName, categoryId)

				yield categoryItem

		nextUrl = response.css('.next::attr(href)').extract_first()
		urlll = parse.urljoin(response.url,nextUrl)
		yield Request(urlll,callback=self.parseDetail)

	def createCateItem(self, topName, middleName, name, Id):
		cateItem = CategoryItem()
		cateItem['topCategoryName'] = topName
		cateItem['middleCategoryName'] = middleName
		cateItem['categoryName'] = name
		cateItem['categoryId'] = Id
		return cateItem

	def createRecipeItem(self, recipeId, categoryIds, recipeName, recipeCoverUrl, point, count, auther):
		recipeItem = RecipeItem()
		recipeItem['recipeId'] = recipeId
		recipeItem['categoryIds'] = categoryIds
		recipeItem['recipeName'] = recipeName
		recipeItem['recipeCoverUrl'] = recipeCoverUrl
		recipeItem['point'] = point
		recipeItem['count'] = count
		recipeItem['auther'] = auther
		return recipeItem
